package cn.itcast.demo;

import cn.itcast.demo.pojo.ElasticConstants;
import cn.itcast.demo.pojo.Goods;
import cn.itcast.demo.repository.GoodsRepository;
import com.leyou.starter.elastic.dto.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsStarterDemoApplicationTests {

    @Autowired
    private GoodsRepository repository;

    @Test
    public void contextLoads() {
        // 创建索引库
        //先删除再创建
        repository.createIndex(ElasticConstants.SOURCE_TEMPLATE);
    }

    // 单个文档的新增，id存在时会修改
    @Test
    public void testAddDocument() {
        //GET /goods/_search
        repository.save(new Goods(1L, Arrays.asList("红米", "手机"), "红米9手机 数码", 1499L));
    }

    // 批量增
    @Test
    public void testAddBatch() {
        List<Goods> list = new ArrayList<>();
        list.add(new Goods(1L, Arrays.asList("红米9", "手机"), "红米9手机 数码", 1499L));
        list.add(new Goods(2L, Arrays.asList("三星 Galaxy", "A90"), "三星 Galaxy A90 手机 数码 疾速5G 骁龙855", 3099L));
        list.add(new Goods(3L, Arrays.asList("Sony", "WH-1000XM3"), "Sony WH-1000XM3 降噪耳机 数码", 2299L));
        list.add(new Goods(4L, Arrays.asList("松下", "剃须刀"), "松下电动剃须刀高转速磁悬浮马达", 599L));
        repository.saveAll(list);
    }

    //根据id删除
    @Test
    public void testDelete() {
        repository.deleteById(1L);
    }

    //根据id查询
    @Test
    public void testGetById() throws InterruptedException {
        log.info("开始查询。。。");
        Mono<Goods> mono = repository.queryById(2L);
        //mono 异步 订阅
        mono.subscribe(System.out::println);
        log.info("查询结束。。。");
        //防止提前结束订阅数据查看不到
        Thread.sleep(2000L);
    }

    //条件查询
    @Test
    public void testQuery() throws InterruptedException {
        // 搜索条件的构建器
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 1.查询条件
        sourceBuilder.query(QueryBuilders.matchQuery("title", "松下"));
        // 2.分页条件
        sourceBuilder.from(0);
        sourceBuilder.size(20);
        // 3.高亮条件
        sourceBuilder.highlighter(new HighlightBuilder().field("title"));

        log.info("开始条件查询");
        Mono<PageInfo<Goods>> mono = repository.queryBySourceBuilderForPageHighlight(sourceBuilder);

        mono.subscribe(info -> {
            long total = info.getTotal();
            System.out.println("total = " + total);
            List<Goods> list = info.getContent();
            list.forEach(System.out::println);
        });
        log.info("结束条件查询");
        Thread.sleep(2000);
    }

    //自动补全
    @Test
    public void testSuggest() throws InterruptedException {
        log.info("开始查询");
        Mono<List<String>> mono = repository.suggestBySingleField("name", "s");
        mono.subscribe(list -> list.forEach(System.out::println));
        log.info("结束查询");
        Thread.sleep(2000);
    }
}